package com.example.smartcitymovie.ui.home;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.adapter.RecycleViewAdapter;
import com.example.smartcitymovie.bean.NewsListBean;
import com.example.smartcitymovie.bean.ResultBean;
import com.example.smartcitymovie.databinding.FragmentKindNewsBinding;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link KindNewsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class KindNewsFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ID = "id";
    private FragmentKindNewsBinding binding;

    // TODO: Rename and change types of parameters
    private int id;

    public KindNewsFragment() {
        // Required empty public constructor
    }


    public static KindNewsFragment newInstance(int id) {
        KindNewsFragment fragment = new KindNewsFragment();
        Bundle args = new Bundle();
        args.putInt(ID, id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getInt(ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentKindNewsBinding.inflate(inflater);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        OkHttpUtils.getCall("/prod-api/api/movie/press/press/list/?sortId="+id).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                NewsListBean newsListBean = MyApplication.getGson().fromJson(response.body().string(),NewsListBean.class);
                if (newsListBean.getCode()==200){
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            binding.RVShowMovieNews.setLinearLayoutManager(LinearLayout.VERTICAL);
                            binding.RVShowMovieNews.setAdapter(newsListBean.getRows(),R.layout.news_list_item_layout);
                            binding.RVShowMovieNews.getBaseAdapter().setOnPartRecycleViewItemClickListener(new RecycleViewAdapter.OnPartRecycleViewItemClickListener() {
                                @Override
                                public void onPartRecycleViewItemClickListener(View view, int position) {
                                    if (MyApplication.getSharedPreferences().getBoolean("isLogin",false)){
                                        TextView textView = view.findViewById(R.id.TV_MovieThumbNums);
                                        if (textView!=null){
                                            int sum = Integer.valueOf(textView.getText().toString()) +1;
                                            textView.setText(Integer.valueOf(sum));
                                            OkHttpUtils.postCall("/prod-api/api/movie/film/comment/like/").enqueue(new Callback() {
                                                @Override
                                                public void onFailure(Call call, IOException e) {

                                                }

                                                @Override
                                                public void onResponse(Call call, Response response) throws IOException {
                                                    ResultBean resultBean = MyApplication.getGson().fromJson(response.body().string(), ResultBean.class);
                                                    getActivity().runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            if (resultBean.getCode()==200){
                                                                Toast.makeText(getActivity(), "点赞成功", Toast.LENGTH_SHORT).show();
                                                            }else{
                                                                Toast.makeText(getActivity(), resultBean.getMsg(), Toast.LENGTH_SHORT).show();
                                                            }
                                                        }
                                                    });
                                                }
                                            });
                                        }
                                    }else{
                                        Toast.makeText(getActivity(), "请先前往登录", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            },R.id.btn_thumb);
                            binding.RVShowMovieNews.getBaseAdapter().setOnRecycleViewItemCLickListener(new RecycleViewAdapter.OnRecycleViewItemCLickListener() {
                                @Override
                                public void onRecycleViewItemCLickListener(View view, int position) {
                                    Intent intent = new Intent(getActivity(),NewsMessageActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putSerializable("bean",newsListBean.getRows().get(position));
                                    intent.putExtra("bundle",bundle);
                                    startActivity(intent);
                                }
                            });
                        }
                    });
                }
            }
        });
    }
}